<?php 
include("util.php");
include("header.php");
?>
<?php 
$count_per_page = 50;
$page = intval($_GET['page']);
if ($page == null || $page < 1) {
  $page = 1;
}

$conn = mysql_connect("127.0.0.1","root","");
mysql_select_db("xingtan");

$sql=sprintf("SELECT count(a.id) AS total FROM articles AS a, authors AS u WHERE a.author = u.id AND a.category >= 0 AND a.level > 20");
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$total_pages = ceil($row['total'] / $count_per_page);

$sql=sprintf("SELECT a.id AS a_id, a.title AS a_title, a.digest AS a_digest, a.create_time AS a_create_time, 
    u.id AS u_id, u.name AS u_name, u.title AS u_title, u.url AS u_url 
    FROM articles AS a, authors AS u WHERE a.author = u.id AND a.category >= 0 AND a.level > 20 ORDER BY a.id DESC limit %d", $page * $count_per_page);
$result = mysql_query($sql);
?>
<div class="container">
	<div class="main">
		<div class="content">
			<div class="articles">
			<div style="height:6px;"></div>
<?php 
CommonPageNav($total_pages, $count_per_page, $page, 4, "/essence/%d/");
print('<div class="article_body">');
$index = 0;
while($row = mysql_fetch_array($result)) {
  if ($index >= ($page - 1) * $count_per_page) {
    ArticlePreview($row['a_id']);
  }
  ++$index;
}
print('</div>');
CommonPageNav($total_pages, $count_per_page, $page, 4, "/essence/%d/");
?>
			</div>
		</div>
	</div>
</div>
<?php include("back-top.php"); ?>
<?php 
mysql_close($conn);
include("footer.php");
?>
